package com.ap.core.bulkupload.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ap.core.bulkupload.service.ManageMerchantService;
import com.ap.core.context.ApplicationContextProvider;
import com.ap.core.cxf.jaxb.managemerchant.RequestMerchantRegistrationType;
import com.ap.core.cxf.jaxb.managemerchant.ResponseMerchantRegistrationType;
import com.ap.core.exception.APCoreException;
import com.ap.core.util.JaxbUtil;
import com.core.ap.wsdl.managemerchantservice_1.ManageMerchant1PortType;
import com.core.ap.wsdl.managemerchantservice_1.RegisterMerchant1Fault;

/**
 * The Class ManageMerchantServiceImpl.
 * @author Siva.Pochana
 */
public class ManageMerchantServiceImpl implements ManageMerchantService {

	private static final Logger LOG = LoggerFactory.getLogger(ManageMerchantServiceImpl.class);
	
	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.service.ManageMerchantService#registerMerchant(com.ap.core.cxf.jaxb.managemerchant.RequestMerchantRegistrationType)
	 */
	@Override
	public ResponseMerchantRegistrationType registerMerchant(RequestMerchantRegistrationType merchantRegRequest) throws APCoreException {
		String methodName = "registerMerchant() - ";
		ResponseMerchantRegistrationType merchantRegResponse;
	
		try {
			LOG.info(methodName + "Invoking the Manage Mercahnt Service.");
			
			JaxbUtil.newInstance().javaToXML(RequestMerchantRegistrationType.class, merchantRegRequest);
			
			ManageMerchant1PortType manageMerchant1PortType = (ManageMerchant1PortType)ApplicationContextProvider.getBean(ManageMerchant1PortType.class);
			merchantRegResponse = manageMerchant1PortType.registerMerchant1(merchantRegRequest);
			
			JaxbUtil.newInstance().javaToXML(ResponseMerchantRegistrationType.class, merchantRegResponse);
			
			LOG.info(methodName + "Merchant registered successfully");
			
			return merchantRegResponse;
		} catch (RegisterMerchant1Fault e) {
			LOG.error(methodName + "RegisterMerchant1Fault occurred :"+e.getMessage());
			throw new APCoreException(e);
		} catch (Exception e) {
			LOG.error(methodName + "Exception occurred :"+e.getMessage());
			e.printStackTrace();
			throw new APCoreException(e);
		}
	}

}
